/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mudalov.rec.dao.hibernate.impl;

import com.mudalov.rec.dao.interfaces.HbnGenericDAO;
import com.mudalov.rec.dao.interfaces.IStudentDAO;
import com.mudalov.rec.dao.model.user.Student;
import com.mudalov.rec.util.StringUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author MU
 */
public class HbnStudentDAO extends  HbnGenericDAO<Student, Long> implements IStudentDAO {

	public HbnStudentDAO(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	protected Class<Student> getEntityClass() {
		return Student.class;
	}

    public Student getStudent(String login, String password) {
        List<Criterion> criterions = new ArrayList<Criterion>();
        criterions.add(Restrictions.eq("password", password));
        criterions.add(Restrictions.eq("login", login));
        return findDetachedObjectByCriteria(criterions);
    }
    
    public List<Student> getStudents(String fullName, String login, String mail, String group) {
                List<Criterion> criterions = new ArrayList<Criterion>();
        if (!StringUtil.areEmpty(fullName)) {
            criterions.add(Restrictions.like("fullName", "%" + fullName + "%"));
        }
        if (!StringUtil.areEmpty(login)) {
            criterions.add(Restrictions.like("login", "%" + login + "%"));
        }
        if (!StringUtil.areEmpty(mail)) {
            criterions.add(Restrictions.like("mail", "%" + mail + "%"));
        }
        if (!StringUtil.areEmpty(group)) {
            criterions.add(Restrictions.like("group", "%" + group + "%"));
        }
        List<Student> result = findDetachedObjectsByCriteria(criterions);
        return result;
    }


    public List<Student> getStudents(String fullName, String login, String mail) {
        return getStudents(fullName, login, mail, "");
    }

    public List<Student> getStudents(String fullName, String group) {
        return getStudents(fullName, "", "", group);
    }
}
